package com.atXiao.sparseArray;

/**
 * 稀疏数组
 */
public class SparseArray {
    public static void main(String[] args) {
        /**
         *
         * 普通数组 int chessArray[11][11] -->转为稀疏数组
         *
         * 1->黑子
         *2->蓝子
         */

        /**
         * 原始棋盘
         */
        int[][] chessArray = new int[11][11];
        chessArray[1][2] = 1;
        chessArray[2][3] = 2;
        chessArray[4][5] = 1;


        System.out.println("长度为:" + chessArray.length);


        for (int[] ints : chessArray) {
            for (int data : ints) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }

        int normalCount = 0;
        int length = 0;


        for (int[] ints : chessArray) {
            for (int data : ints) {
                length = ints.length;
                if (data != 0) {
                    normalCount++;
                }
            }
        }
        System.out.println("原始棋盘中不为0的个数为:" + normalCount);

        /**
         * 稀疏数组定义
         */

        int[][] sparseArray = new int[normalCount + 1][3];
        //第一行第一列
        sparseArray[0][0] = chessArray.length;
        sparseArray[0][1] = length;
        sparseArray[0][2] = normalCount;


//        for (int i = 0; i < chessArray.length; i++) {
//            for (int j = 0; j < chessArray[0].length; j++) {
//
//            }
//
//        }

        int hang = 0;
        int tmpCount = 0;
        for (int[] ints : chessArray) {

            int lie = 0;
            for (int data : ints) {

                //列
                if (data != 0) {
                    tmpCount++;
                    sparseArray[tmpCount][0] = hang;
                    sparseArray[tmpCount][1] = lie;
                    sparseArray[tmpCount][2] = data;


                }
                lie++;
            }
            hang++;
        }

        System.out.println("转换后的稀疏数组");

        for (int[] ints : sparseArray) {
            for (int data : ints) {

                System.out.printf("%d\t", data);
            }
            System.out.println();
        }


    }

}

